package com.dev.utils.app.assist;

import com.dev.DevUtils;
import com.dev.utils.LogUtils;
import com.dev.utils.app.AppUtils;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.ResourceManager;

/**
 * detail: Resources 辅助类
 *
 * @author Ttt
 * <pre>
 *     整合 DevApp Utils 代码, 最终统一通过该辅助工具类进行 Resources 获取
 *     方便对 Resources 进行适配、统一控制、代码复用等
 * </pre>
 */
public class ResourceAssist {
    // ResourceAssist 实例
    private static volatile ResourceAssist sInstance;

    // Resources
    private ResourceManager mResource;

    // 应用包名
    private String    mPackageName;

    /**
     * 获取 ResourceAssist 实例
     *
     * @return {@link ResourceAssist}
     */
    public static ResourceAssist getInstance() {
        if (sInstance == null) {
            synchronized (ResourceAssist.class) {
                if (sInstance == null) {
                    sInstance = new ResourceAssist();
                }
            }
        }
        return sInstance;
    }

    // 日志 TAG
    private static final String TAG = ResourceAssist.class.getSimpleName();

    // ===========
    // = 构造函数 =
    // ===========

    private ResourceAssist() {
        this(staticResources(), AppUtils.getPackageName());
    }

    private ResourceAssist(final Context context) {
        this(staticResources(context), AppUtils.getPackageName());
    }

    private ResourceAssist(final ResourceManager resource) {
        this(resource, AppUtils.getPackageName());
    }

    private ResourceAssist(
            final ResourceManager resource,
            final String packageName
    ) {
        this.mResource = resource;
        this.mPackageName = packageName;
    }



    /**
     * 获取 Resources
     *
     * @return {@link ResourceManager}
     */
    public ResourceManager getResources() {
        return mResource;
    }

    // ===========
    // = 静态方法 =
    // ===========

    /**
     * 获取 Resources
     *
     * @return {@link ResourceManager}
     */
    public static ResourceManager staticResources() {
        return staticResources(DevUtils.getContext());
    }

    /**
     * 获取 Resources
     *
     * @param context {@link Context}
     * @return {@link ResourceManager}
     */
    public static ResourceManager staticResources(final Context context) {
        if (context == null) return null;
        try {
            return context.getResourceManager();
        } catch (Exception e) {
            LogUtils.error(TAG, e + "staticResources");
        }
        return null;
    }


    /**
     * 获取 Color
     * <pre>
     *
     * </pre>
     *
     * @param colorId R.color.id
     * @return Color
     */
    public int getColor(final int colorId) {
        try {
            return mResource.getElement(colorId).getColor();
        } catch (Exception e) {
            LogUtils.error(TAG, e + "error getColor");
        }
        return -1;
    }

    /**
     * 获取 Color
     * <pre>
     *
     * </pre>
     *
     * @param colorId R.color.id
     * @return Color
     */
    public Color getColorForColor(final int colorId) {
        try {
            return new Color(mResource.getElement(colorId).getColor());
        } catch (Exception e) {
            LogUtils.error(TAG, e + "getColor");
        }
        return null;
    }


}
